package com.ccs.demo;


import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpServerErrorException;

import java.io.PrintStream;

@RequestMapping("/request2")
@RestController
public class RequestController2 {

    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies =request.getCookies();
        if(cookies != null) {
            Cookie[] var3 = cookies;
            int var4 = cookies.length;

            for(int var5=0;var5<var4;++var5){
                Cookie cookie=var3[var5];
                PrintStream var1000=System.out;
                String var10001=cookie.getName();
                var1000.println(var10001 + ":" + cookie.getValue());
            }
        }
        return "获取cookie成功";
    }

//    @RequestMapping("/getCookie2")
//
//    public String getCookie2(@CookieValue String className){
//        return "获取cookie成功，className:"+className;
//    }

    @RequestMapping({"/getCookie2"})
    public String getCookie2(@CookieValue("class") String className) {
        return "获取cookie成功, className:" + className;
    }

        @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request,String name,Integer age){
        HttpSession session=request.getSession();
        session.setAttribute("name",name);
        session.setAttribute("age",age);
        return "设置session成功";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session =request.getSession();
        String name= (String) session.getAttribute("name");
        Integer age=(Integer) session.getAttribute("age");
        return "name:" + name +" age:" +  age;
    }


}
